/**
 * 广告合约
 */
angular.module('imsApp').factory('AdContractsManager', ['$http','api', 'paramsManager', 'FileUploader', function ($http,api, paramsManager, FileUploader) {
    var AdContractsManager = {
        /**
         * 获取广告合约列表
         */
        adContractsList: function (params, query) {
            return $http.get(api+'/ims/adContracts/' + paramsManager.params(params) + paramsManager.query(query)).then(function (result) {
                return result;
            });
        },
        /**
         *广告合约添加
         * @returns {*}
         */
        adContractAdd: function (params) {
            return $http.post(api+'/ims/adContracts', params).then(function (result) {
                return result;
            });
        },
        /**
         *广告合约详情
         * @param channelContractId
         * @returns
         */
        adContractDetail: function (id) {
            return $http.get(api+'/ims/adContracts/' + id).then(function (result) {
                return result;
            });
        },
        /**
         *广告合约修改
         * @param channelContractId
         *
         */
        adContractEdit: function (channelContractId, params) {
            return $http.put(api+'/ims/adContracts/' + channelContractId, params).then(function (result) {
                return result;
            });
        },
        /**
         *广告合约上传
         * @param params
         */
        adContractUpload: function (params) {
            return $http.post(api+'/ims/adContracts/image/photoFile', params).then(function (result) {
                return result;
            });
        },
        /**
         *广告合约删除
         * @param params advertiserId,{status:11}
         * @returns {"c":"f"} ?{"c": "f","i":"修改失败"}
         */
        adContractDelete: function (ids) {
            console.log(ids,"ss")
            return $http.delete(api+'/ims/adContracts/'+ids.join(",")).then(function (result) {
                return result;
            });
        },
        /**
         *广告合约文件修改顺序
         * @param params advertiserId,{status:11}
         * @returns {"c":"f"} ?{"c": "f","i":"修改失败"}
         */
        adContractSort: function (id, scansPathList) {
            return $http.put(api+'/ims/adContracts/' + id + '/image', scansPathList).then(function (result) {
                return result;
            });
        },
        /**
         *关联广告合约
         *
         */
        adContractRelation: function (params) {
            return $http.get(api+'/ims/simple/advContracts/' + paramsManager.params(params)).then(function (result) {
                return result;
            });
        },
        getImage: function ($scope) {
            var MAXSIZE = 1;//单张图片最大大小：单位M
            var MAXITEM = 20;//上传张数限制
            var image = $scope.uploaderImage = new FileUploader({
                 alias:"photoFile",
                 queueLimit: 20,//文件个数
                 // removeAfterUpload: true,//上传后删除文件 */
                filters: [{
                    name: 'photoFile',//后台接收字段
                    fn: function (item /*{File|FileLikeObject}*/, options) {
                        if (!/\.(jpg|jpeg|png|JPG|PNG)$/.test(item.name)) {
                            $scope.checkImage = "E";
                            $scope.imageMsg = "最多只能上传" + MAXITEM + "张png、jpg格式且每张小于" + MAXSIZE + "MB的图片";
                            return false;
                        }
                        if (this.queue.length > MAXITEM - 1) {
                            $scope.checkImage = "E";
                            $scope.imageMsg = "最多只能上传" + MAXITEM + "张png、jpg格式且每张小于" + MAXSIZE + "MB的图片";
                            return false;
                        } else {
                            if (item.size > MAXSIZE * 1024 * 1024) {
                                $scope.checkImage = "E";
                                $scope.imageMsg = "最多只能上传" + MAXITEM + "张png、jpg格式且每张小于" + MAXSIZE + "MB的图片";
                                return false;
                            } else {
                                //去重
                                if (this.queue.length > 0) {
                                    var isExsit = false;
                                    for (var i = 0; i < this.queue.length; i++) {
                                        if (this.queue[i].file.name == item.name) {
                                            isExsit = true;
                                            break;
                                        }
                                    }
                                    if (isExsit) {
                                        return false;
                                    } else {
                                        $scope.uploadSuccess = false;
                                        return true;
                                    }
                                } else {
                                    $scope.uploadSuccess = false;
                                    return true;
                                }
                            }
                        }
                    }
                }],
                url: api+"/ims/adContracts/image",
                headers: {'X-Auth-Token': $scope.userInfo.token}

            });
            return image;
        },
        uploadFile: function ($scope, image, id) {
            if (image.queue.length > 0) {
                //上传
                image.uploadAll();
                image.onErrorItem = function (fileItem, response, status, headers) {
                    alert("网络出现故障，上传失败,请刷新重新添加");   //未达到后台上传失败
                };
                //成功上传
                var successNo = 0;
                image.onSuccessItem = function (fileItem, response, status, headers) {
                    if (response.c == "s") {
                        successNo++;
                        if (!id) {//新建
                            $scope.formData.scansPathList = $scope.formData.scansPathList=="undefined" ||$scope.formData.scansPathList==null  ? response.fileVO.webUrl : $scope.formData.scansPathList + "," + response.fileVO.webUrl;
                            
                        } else {//修改
                            if (successNo == 1) {//修改时上传了第一张，应当将之前的路径拼接串覆盖
                                $scope.formData.scansPathList = response.fileVO.webUrl;
                            } else {
                                $scope.formData.scansPathList = $scope.formData.scansPathList + "," + response.fileVO.webUrl;
                            }

                        }
                        if (successNo == image.queue.length) {
                            $scope.uploadSuccess = true;
                        }
                    } else if (response.c == 'f') {
                        fileItem.isSuccess = false;
                        alert("上传  " + fileItem.file.name + " 失败，请删除后重新添加上传");
                    } else {
                        fileItem.isSuccess = false;
                        alert(response.i);
                    }
                };
            }
        }
    }

    return AdContractsManager;
}]);
